package com.hrb.douban.activtity;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.hrb.douban.CustomApplication;
import com.hrb.douban.R;
import com.hrb.douban.activtity.base.BaseActivity;
import com.hrb.douban.adapter.HotWordAdapter;
import com.hrb.douban.utils.ToastUtil;

import butterknife.BindView;
import butterknife.ButterKnife;

import static com.hrb.douban.CustomApplication.getContext;

public class SearchPageActivity extends BaseActivity implements View.OnClickListener {

    @BindView(R.id.search_back_icons)
    ImageView searchBackIcons;
    @BindView(R.id.search_category_btn)
    Button searchCategoryBtn;
    @BindView(R.id.search_action_btn)
    Button searchActionBtn;
    @BindView(R.id.search_edit_input)
    EditText searchEditInput;
    @BindView(R.id.search_recycler)
    RecyclerView searchRecycler;
    HotWordAdapter adapter;
    HotWordAdapter keyadapter;
    public String hotKeyword[] = {"功夫熊猫", "烈日灼心", "摆渡人", "长城", "我不是潘金莲", "这个杀手不太冷", "驴得水",  "西游伏妖篇", "海贼王之黄金城","我在故宫修文物", "你的名字"};
    public String hotKeywordSearch[] = {"爱情", "惊悚", "剧情", "喜剧"};
    @BindView(R.id.search_key_recycler)
    RecyclerView searchKeyRecycler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_page);
        ButterKnife.bind(this);
        searchBackIcons.setOnClickListener(this);
        searchActionBtn.setOnClickListener(this);
        searchCategoryBtn.setOnClickListener(this);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), 4);
        searchRecycler.setLayoutManager(gridLayoutManager);
        adapter = new HotWordAdapter(hotKeyword);
        searchRecycler.setAdapter(adapter);
        gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int i) {
                return setSpanSize(i, hotKeyword);
            }
        });
        GridLayoutManager glm = new GridLayoutManager(getContext(), 4);
        keyadapter = new HotWordAdapter(hotKeywordSearch);
        searchKeyRecycler.setLayoutManager(glm);
        searchKeyRecycler.setAdapter(keyadapter);


    }

    private int setSpanSize(int position, String[] keywords) {
        int count;
        if (keywords[position].length() > 5) {
            count = 2;
        } else {
            count = 1;
        }

        return count;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.search_back_icons:
                /*Intent backIntent = new Intent(this, MainActivity.class);
                backIntent.putExtra("name", 1);
                //FilmDetailsActivity.this.setResult(ConstantCode.resultCode, backIntent);
                startActivity(backIntent);*/
                finish();
                break;
            case R.id.search_action_btn:
                String category= searchEditInput.getText().toString();
                if("".equals(category)||category==null){
                    new AlertDialog.Builder(this).setTitle("提示").setMessage("搜索内容为空！").setPositiveButton("我知道了", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            searchEditInput.setFocusable(true);
                        }
                    }).create().show();
                }else{
                    Intent intent=new Intent(CustomApplication.getContext(),SearchResultActivity.class);
                    intent.putExtra("category",category);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                }

                break;
            case R.id.search_category_btn:
                showBottomDialog();
                break;
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            /*Intent backIntent = new Intent(this, MainActivity.class);
            backIntent.putExtra("name", 1);
            //FilmDetailsActivity.this.setResult(ConstantCode.resultCode, backIntent);
            startActivity(backIntent);*/
            finish();

        }
        return true;

    }

    private void showBottomDialog() {
        //1、使用Dialog、设置style
        final Dialog dialog = new Dialog(this, R.style.DialogTheme);
        //2、设置布局
        View view = View.inflate(this, R.layout.dialog_custom_layout, null);
        dialog.setContentView(view);

        Window window = dialog.getWindow();
        //设置弹出位置
        window.setGravity(Gravity.BOTTOM);
        //设置弹出动画
        window.setWindowAnimations(R.style.main_menu_animStyle);
        //设置对话框大小
        window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        dialog.show();

        dialog.findViewById(R.id.tv_take_default).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                searchCategoryBtn.setText("默认");
                searchEditInput.setHint("输入电影标题、演员或是导演");
                dialog.dismiss();
            }
        });

        dialog.findViewById(R.id.tv_take_category).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                searchCategoryBtn.setText("类型");
                searchEditInput.setHint("请输入影片类型，如：爱情、喜剧");
                dialog.dismiss();
            }
        });

        dialog.findViewById(R.id.tv_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });

    }
}
